//
//  WMECollectViewController.m
//  everyDay_duiTang
//
//  Created by 恩叔 on 16/2/29.
//  Copyright © 2016年 enshu. All rights reserved.
//

#import "WMECollectViewController.h"
#import "DataBaseManager.h"
#import "WMEFirstPageInformationVC.h"

@interface WMECollectViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) UITableView *collecttableView;
@property (nonatomic, retain) NSMutableArray *arr; /**< 数据库数组 */

@end

@implementation WMECollectViewController
- (void)dealloc
{
    [_collecttableView release];
    [_arr release];
    [super dealloc];
}
- (void)viewWillAppear:(BOOL)animated
{
    if (!_arr.count) {
        UILabel *kong = [[UILabel alloc] initWithFrame:self.view.bounds];
        kong.text = @"亲, 没有收藏哦";
        kong.tag = 5010;
        kong.textAlignment = NSTextAlignmentCenter;
        [self.view addSubview:kong];
        [kong release];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"back" style:UIBarButtonItemStylePlain target:self action:@selector(fanhuiqu:)];
    self.navigationItem.title = @"我的收藏列表";
    // 打开表
    DataBaseManager *dbManger = [DataBaseManager shareInstance];
    [dbManger openDB];
    self.arr = [[DataBaseManager shareInstance] select];
    NSLog(@"%@", _arr);
    
    
    self.collecttableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    _collecttableView.delegate = self;
    _collecttableView.dataSource = self;
    [self.view addSubview:_collecttableView];
    [_collecttableView release];
    [_collecttableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (_arr.count) {
        UILabel *qing = [self.view viewWithTag:5010];
        [qing removeFromSuperview];
    }
    return _arr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    Movie *m = _arr[indexPath.row];
    cell.textLabel.text = m.movieName;
    return cell;
}
// 点击跳转方法
- (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    WMEFirstPageInformationVC *informationView = [[[WMEFirstPageInformationVC alloc] init] autorelease];
    informationView.hidesBottomBarWhenPushed = YES;
    Movie *m = _arr[indexPath.row];
    // 字符串转NSNumber
    NSNumberFormatter *f = [[NSNumberFormatter alloc] init];
    id result = [f numberFromString:m.pubdate];
    
    informationView.jiekou = [result stringValue];
    [self.navigationController pushViewController:informationView animated:YES];
    [f release];
}
// 允许编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return YES;
}
// 通过编辑状态判断编辑操作
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 通过参数editingStyle判断 执行不同操作
    // 删除操作
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // 删除 分两个部分 数据/视图
        // 先操作数据 再同步更新视图
        // 1.从数组中 删除数据
        Movie *m = _arr[indexPath.row];
        [[DataBaseManager shareInstance] deleteMovieWithName:m.movieName];
        [_arr removeObjectAtIndex:indexPath.row];

        // 2.同步视图
        // 参数1: 试图操作cell 的位置
        // 参数2: 操作的动画选项
        [_collecttableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
        [_collecttableView reloadData];
    }
    if (!_arr.count) {
        UILabel *kong = [[UILabel alloc] initWithFrame:self.view.bounds];
        kong.text = @"亲, 没有收藏哦";
        kong.tag = 5010;
        kong.textAlignment = NSTextAlignmentCenter;
        [self.view addSubview:kong];
        [kong release];
    }
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 默认编辑样式为 删除
    //    return UITableViewCellEditingStyleInsert; // 添加/插入
    //    return UITableViewCellEditingStyleInsert | UITableViewCellEditingStyleDelete;
    return UITableViewCellEditingStyleDelete;
}

- (void)fanhuiqu:(UIBarButtonItem *)bar
{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
